import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/9
 * Time: 10:11
 * Description:
 */

/*class Card implements Comparable<Card>{
    public int rank; // 数值
    public String suit; // 花色
    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return rank == card.rank &&
                suit.equals(card.suit);
    }
    @Override
    public int compareTo(Card o) {
        return this.rank-o.rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "rank=" + rank +
                ", suit='" + suit + '\'' +
                '}';
    }
}*/

class Card implements Comparable<Card>{
    public int rank; // 数值
    public String suit; // 花色
    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return rank == card.rank &&
                suit.equals(card.suit);
    }
    @Override
    public int compareTo(Card o) {
        return o.rank-this.rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "rank=" + rank +
                ", suit='" + suit + '\'' +
                '}';
    }
}
class RankComparator implements Comparator<Card> {
    @Override
    public int compare(Card o1, Card o2) {
        return o1.rank-o2.rank;
    }
}
class SuitComparator implements Comparator<Card> {
    @Override
    public int compare(Card o1, Card o2) {
        return o1.suit.compareTo(o2.suit);
    }
}

public class TestDemo {

    public static void main(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();
        testHeap.createHeap(array);
        //System.out.println("排序之前："+Arrays.toString(array));

        testHeap.heapSort();
        System.out.println("fdsasa");
        //System.out.println("排序之后："+Arrays.toString(array));
    }


    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        List<List<Integer>> lists = new ArrayList<>();

        PriorityQueue<List<Integer>> maxHeap = new PriorityQueue<>(k,new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                return (o2.get(0) + o2.get(1))-(o1.get(0)+o1.get(1));
            }
        });

        for (int i = 0; i < Math.min(nums1.length,k); i++) {
            for (int j = 0; j < Math.min(nums2.length,k); j++) {
                if(maxHeap.size() < k) {
                    List<Integer> tmp = new ArrayList<>();
                    tmp.add(nums1[i]);
                    tmp.add(nums2[j]);
                    maxHeap.offer(tmp);
                }else {
                    List<Integer> top = maxHeap.peek();
                    int val = top.get(0)+top.get(1);//堆顶的值
                    if(val > nums1[i] + nums2[j] ) {
                        maxHeap.poll();//先弹出去
                        List<Integer> tmp = new ArrayList<>();
                        tmp.add(nums1[i]);
                        tmp.add(nums2[j]);
                        maxHeap.offer(tmp);
                    }
                }
            }
        }

        for (int i = 0; i < k && !maxHeap.isEmpty(); i++) {
            List<Integer> tmp = maxHeap.poll();
            lists.add(tmp);
        }
        return lists;
    }



    public static void main7(String[] args) {
        Card card1 = new Card(2,"红桃");
        Card card2 = new Card(10,"红桃");

        //RankComparator rankComparator = new RankComparator();

        //PriorityQueue<Card> priorityQueue = new PriorityQueue<>(rankComparator);
        PriorityQueue<Card> priorityQueue = new PriorityQueue<>(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.rank-o1.rank;
            }
        });

        PriorityQueue<Card> priorityQueue2 = new PriorityQueue<>((x1,x2)->{return x1.rank-x2.rank;});

        priorityQueue.offer(card1);
        priorityQueue.offer(card2);

        System.out.println(priorityQueue);
    }


    public static void main6(String[] args) {
        Card card1 = new Card(2,"红桃");
        Card card2 = new Card(10,"红桃");

        PriorityQueue<Card> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(card1);
        priorityQueue.offer(card2);

        System.out.println(priorityQueue);
    }

    public static void main5(String[] args) {
        Card card1 = new Card(10,"红桃");
        Card card2 = new Card(2,"红桃");
        //System.out.println(card1.compareTo(card2));

        RankComparator rankComparator = new RankComparator();
        System.out.println(rankComparator.compare(card1, card2));
    }


    public static void main4(String[] args) {
        Card card1 = new Card(1,"红桃");
        Card card2 = new Card(1,"红桃");
        System.out.println(card1 == card2);
        System.out.println(card1.equals(card2) );
    }

    public static void topk(int[] array,int k) {

        PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);

        for (int i = 0; i < array.length; i++) {
            if(minHeap.size() < k) {
                minHeap.offer(array[i]);
            }else {
                int top = minHeap.peek();
                if(top < array[i]) {
                    minHeap.poll();
                    minHeap.offer(array[i]);
                }
            }
        }
        for (int i = 0; i < k; i++) {
            System.out.print(minHeap.poll()+" ");
        }
    }

    public static void main3(String[] args) {
        int[] array = {12,54,2,78,31,99,67};
        topk(array,3);
    }

    public static void main2(String[] args) {
        //小堆  默认容量是11
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(13);
        priorityQueue.offer(2);
        priorityQueue.offer(5);

        System.out.println(priorityQueue.poll());
    }

    public static void main1(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();
        testHeap.createHeap(array);
        testHeap.push(80);
        //System.out.println("fafafafa");
        System.out.println(testHeap.pop());
        System.out.println("fafafafa");
        System.out.println(testHeap.peek());
    }
}
